<div class="tableform">
  <h3>在线盘点</h3>
  <form action="index.php?app=taoguaninventory&ctl=admin_inventorylist&act=create_inventory" method="post" id="onlineForm">
    <input type="hidden" name="branch_id" id="branch_id" value="<{$inventory.branch_id}>" />
	<input type="hidden" name="inventory_id" value="<{$inventory.inventory_id}>">
    <input type="hidden" name="in_online_inv"  value="1" />
<input type="hidden" id="pos" name="pos" value="<{$pos}>">
    <div class="division">
      <div>
	  <table border="0">
	  <tr>
          <td width="20%"><b>盘点名称</b>：<{$inventory.inventory_name}></td>
          <td width="20%"><b>业务时间</b>:<{$inventory.add_time|date_format:"%Y-%m-%d"}></td>
          <td><b>系统时间</b>：<{$date}></td>
          <td><b>仓库</b>：<{$inventory.branch_name}></td>
          <td><b>盘点人</b>：<{$inventory.op_name}></td>
	  </tr>
        <tr>
          <td width="20%"  colspan="5"><b>盘点类型</b>:<{if $inventory.inventory_type=='2'}>全盘<{elseif $inventory.inventory_type=='3'}>部分盘点<{elseif $inventory.inventory_type=='4'}>期初<{/if}>  &nbsp;&nbsp;<b>备注</b>：<{$inventory.memo|default:'-'}></td>
         
        </tr>
      </table>
        <table width="100%">
		    
		  
           <tr>
            <td colspan="2">基础物料名称：<strong id="goods_name" style="font-size: 26px;"></strong> &nbsp;&nbsp;<strong class="blue" id="goods_spec" style="font-size: 26px;"></strong></td>
          </tr>
        </table>
      </div>
      <div class="division scan_input">
	  <{if $pos==1}>
		<label for="label">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;货位:</label>
        <input type="text" id='pos_ipt' name='pos_name' size='45' vtype="required">
        <{help}><{t}><b>操作提示：</b>手动输入信息，完成后需要按Enter键(回车)提交数据，才可进行下一步操作。<{/t}><{/help}><br>
		<{/if}>
        <label for="label">
        <select name='selecttype' id='selecttype'>
         
        <option value='barcode'>条码:</option>
       <option value='bn'>基础物料编码:</option>
        </select>:
        </label>
        <input type="text" name="barcode" id="bar_ipt" size="45" vtype="required" />
        <{help}><{t}><b>操作提示：</b>手动输入信息，完成后需要按Enter键(回车)提交数据，才可进行下一步操作。<{/t}><{/help}>
        <input type="hidden" id="pro_id" name="product_id" value="" /><br />
        <label for="label">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;数量:</label>
        <input type="text" name="num" id="num_ipt" size="15" vtype="required&&unsigned" disabled="disabled" /> <input type="hidden" name="number" value="" />
      <span id="asso_pos_el"></span>
      </div>
     
        <table border="0" class="inventory_info" style="width:70%;display:none">
         
          <!-- <tr>
            <td>盘点商品货号：<strong id="goods_bn"></strong></td>
            <td width="130">库存总数：<strong id="goods_num"></strong></td>
          </tr> -->
          <tr>
            <td colspan="2"><table class="gridlist" style="width:100%; display:none">
                <thead>
                  <tr>
                    <th>账面数量</th>
                    <th>实际数量</th>
                    <th>盈(+)亏(-)记录</th>
                  </tr>
                </thead>
                <tbody id="dataNode">
                </tbody>
              </table></td>
          </tr>
        </table>
       

      
     
      <div class="table-action"> <{button label="确认" type="button" id="btn_submit" disabled='disabled'}> &nbsp; &nbsp; <{button label="清空" id="btn_reset" type="reset"}> </div>
    </div>
  </form>
  <table width='100%' cellspacing="0" cellpadding="0" class="gridlist">
            <thead>
            <tr>
                <th>基础物料名称</th>
                <th>基础物料编码</th>
                <th>规格</th>
                <th colspan="2">单位</th>
               <th>帐面数量</th>
                <th>实际数量</th>
                <th>盈(+)亏(-)记录</th>
                <th colspan="3" style='display:none'>单价</th>
              </tr>
            </thead>
            <{foreach from=$items item=items key=keys}>
            <tbody>
            <tr class="toggler atStart" >
                <td><{$items.name}></td>
                <td><{$items.bn}></td>
                <td><{$items.spec_info|default:'-'}></td>
                <td colspan="2"><{$items.unit|default:'-'}></td>
                <td><{$items.accounts_num}></td>
                <td><a href="index.php?app=taoguaninventory&ctl=admin_inventorylist&act=detail_inventory_object&item_id=<{$items.item_id}>&product_id=<{$items.product_id}>" target="dialog::{width:700,height:600,title:'盘点明细'}"><{$items.actual_num}></a></td>
                <td><{$items.shortage_over}></td>
                <td colspan="3" style='display:none'><{$items.price|cur}></td>
              </tr>
            </tbody>
            <{/foreach}>
            <tbody >
            <tr style="display:none">
               <td><b>小计：</b></td>
               <td>-</td>
               <td>-</td>
               <td>-</td>
                <td>-</td> 
               <td><{$subtotal.accounts_num}></td>
               <td><{$subtotal.actual_num}></td>
               <td><{$subtotal.shortage_over}></td>
               <td colspan="2">-</td>
              </tr>
            </tbody>
            <tbody >
            <tr >
               <td><b>总计：</b></td>
               <td>-</td>
               <td>-</td>
               <td>-</td>
               <!-- <td>-</td> -->
               <td></td>
               <td><{$total.accounts_num}></td>
               <td><{$total.actual_num}></td>
               <td><{$total.shortage_over}></td>
               <td>-</td>
            </tr>
            </tbody>
        </table>
<div class="gridlist-footer">
<{$pager}>&nbsp;&nbsp;共<{$count}>条，每页显示<{$pagelimit}>条 
</div>		
</div>
<script>
  (function(){
    var node = $('dataNode'),
    bar_ipt = $('bar_ipt'),
    pos_ipt = $('pos_ipt'),

    num_ipt = $('num_ipt'),
    //more = $('more_info'),
    //delItem = more.getElement('.flt input[type=checkbox]'),
    submit = $('btn_submit'),
    reset = $('btn_reset');

	if($('pos').value==1){
		pos_ipt.focus();
	}else{
		bar_ipt.focus();
	}

    function message(msg,el,type,delay,id){
      msg = msg || '';
      type = type || 'error';
      delay = $chk(delay) ?delay : 4000;
      id = id || 'error_msg';
      var bool = type == 'show' ? null : type == 'success' ? true : false;
      var err;
      err = $(id) || new Element('span.'+type+'#'+id);
      err.setHTML(msg).injectAfter(el);
      if(delay) err.destroy.delay(delay,err);
      if(!bool) try{el.select();}catch(e){}
      return bool;
    }
    function validData(ipt,msg,type){
      type = type || 'trim';
      if (!ipt.value) return message('请输入' + msg,ipt);
      //if (type == 'trim' && ipt.value.test(/^[\s 　]+|[\s 　]+$/)) return message(msg + '信息无法识别',ipt);
      if (type == 'number' && !ipt.value.test(/^\d+$/)) return message(msg + '请输入数字',ipt);
      return true;
    }
    function getEmpty(){
      return '<tr><td colspan="4" class="empty">没有货位信息，可在线盘点新建货位！</td></tr>';
    }
    function getDataTpl(j){
      return '<tr key='+j.product_id+' class="associated"><td><span class="hasstore" style="display:none;">'+j.num+'</span><span class="store">-</span></td><td class="num fb">-</td><td class="diff fontcolorGreen">-</td></tr>';
    }
    function requestData(json){
      json = json || node.retrieve('jsondata') || {};
      var data = '';
      
      data = getDataTpl(json);

      node.setHTML(data);

     

      num_ipt.set('disabled',false).focus();
      MessageBox.success('加载成功');
    }
	if($('pos').value==1){
	pos_ipt.addEvent('enter',function(e){
		if(!validData(this,'货位')) return;
		bar_ipt.focus();
	});
	}
    bar_ipt.addEvent('enter',function(e){
      //if(e.key == 'enter'){
	  if($('pos').value=='1'){
	  var pos_ipt = $('pos_ipt').value;
	  }
        cleanup(true);
        setBtn(1);
        submitData();
        ///num_ipt.set('disabled',true);
        if(!validData(this,'条码')) return;
		if($('pos').value=='1'){
		$('pos_ipt').value=pos_ipt;
		}
        new Request({url:'index.php?app=taoguaninventory&ctl=admin_inventory&act=getProduct', method:'post',data: {inventory_id:"<{$inventory.inventory_id}>",barcode: this.value.trim(), branch_id:$('branch_id').value,selecttype:$('selecttype').value},
          onRequest: function(){
            MessageBox.show('正在加载...');
          },
          onSuccess:function(json){
            if(json == 'false'){
              message('商品信息在系统中不存在',this);
              return MessageBox.error('加载失败');
            }
            json = JSON.decode(json);
            if(json.enable == 'false'){
               return MessageBox.error('此盘点单可能已被其他账号确认！');
            }
            node.store('jsondata',json);
            $E('input[name=product_id]').value = json.product_id;
            $('goods_name').setText(json.name);
            $('goods_spec').setText(json.spec_info);
            
            if(json.asso_pos){
            	var html = '';
            	json.asso_pos.each(function(item){
            		//html += '<input type="checkbox" name="asso_pos[]" value="'+item.pos_id+'" checked="checked" />'+item.pos_name+' &nbsp ';
            		html += '<span title="'+item.create_time+'">' +item.pos_name+'</span> &nbsp ';
            		
            	});
              // $('asso_pos_el').setHTML(html);
            }
            //$('goods_bn').setText(json.bn);
            //$('goods_num').setText(json.num);
            requestData(json);
            if((tr = node.getElement('tr[key='+json.product_id+']'))){
                selectbind(tr,true);
            }
          }.bind(this)
        }).send();
      //}
    });
    function selectbind(tr,bool){
      tr.addClass('selected').getSiblings('.selected').removeClass('selected');
      num_ipt.addEvent('enter',function(e){
        //if(e.key == 'enter'){
          process(this,tr,bool);
        //}
      });
    }
    function process(num_ipt,tr,bool){
      if(!validData(num_ipt,'数量','number')){
        tr.getElement('.num').setText('-');
        tr.getElement('.diff').setText('-');
        return false;
      }
      if(tr.match('.selected')){
        tr.getElement('.hasstore').show();
        tr.getElement('.store').hide();
        tr.getElement('.num').setText(num_ipt.value.toInt());
        
        var diff_num = num_ipt.value.toInt() - tr.getElement('.hasstore').get('text').toInt();
        var diff_color = 'red';
        if(diff_num > 0){
            diff_color = 'fontcolorGreen';
        }
        
        tr.getElement('.diff').removeClass('fontcolorGreen');
        tr.getElement('.diff').addClass(diff_color);
        tr.getElement('.diff').setText(num_ipt.value.toInt() - tr.getElement('.hasstore').get('text').toInt());
      }
      $E('input[name=number]').value = num_ipt.value;
      num_ipt.set('disabled',true);
      if(bool){
        setBtn();
      }
      else submit.set('disabled',false).focus();
      submit.removeEvents('click').addEvent('click',function(){
          this.disabled=true;
        this.getParent('form').fireEvent('submit',{stop:$empty});
      });

      return true;
    }
    function init(el){
      if(!el) return;
      el.getElement('.hasstore').hide();
      el.getElement('.store').show();
      el.getElement('.num').setText('-');
      el.getElement('.diff').setText('-');
    }

   

    //submitData();

    reset.addEvent('click',function(){
      cleanup();
      setBtn(1);
      submitData();
    });

    function submitData(bool){
      submit.removeEvents('click').addEvent('click',function(e){
        if(bool){
          //var json = node.retrieve('jsondata').item || {};
          var diff_color = 'red';
          if(num_ipt.value.toInt()>0){
        	  diff_color = 'fontcolorGreen';
          }
          var tpl='<td><span class="hasstore">0</span><span class="store" style="display:none;">-</span></td><td class="num fb">'+num_ipt.value.toInt()+'</td><td class="diff '+ diff_color +'">'+num_ipt.value.toInt()+'</td>';
          var newtr= $E('tr[key='+$E('input[name=product_id]').value+']') || new Element('tr[key='+$E('input[name=product_id]').value+'].noassociated').inject(node,'top');
          newtr.setHTML(tpl);
          selectbind(newtr);
        }else if(!process(num_ipt,node.getElement('tr[key = '+$E('input[name=product_id]').value+']'),true)) return;
        setBtn();
        num_ipt.set('disabled',true);
        $E('[name=number]').set('value',num_ipt.value);
        submit.removeEvents('click').addEvent('click',function(){
            this.disabled=true;
          this.getParent('form').fireEvent('submit',{stop:$empty});
        });
      });
    }
    function cleanup(bool){
      if(bool){
        $$('#onlineForm input[type=text]').erase(bar_ipt).set('value','');
      }
      else {
        $$('#onlineForm input[type=text]').set('value','');
        node.setHTML('');
      }
    }
    function setBtn(bool){
      var v;
      if(bool) v = ['确认','清空','blur'];
      else v = ['确认','返回','focus'];
      submit.set('html','<span><span>' + v[0] + '</span></span>').set('disabled',!!bool)[v[2]]();
      reset.set('html','<span><span>' + v[1] + '</span></span>');
    }
  })();
</script>
